package com.genius.aspect;

import com.genius.annotation.PreInnerAuth;
import com.genius.constant.SecurityConstants;
import com.genius.enums.ResponseType;
import com.genius.exception.GraceException;
import com.genius.utils.ServletUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * 内部服务调用验证处理
 */
@Aspect
@Component
public class PreInnerAuthAspect implements Ordered {
    @Around("@annotation(preInnerAuth)")
    public Object innerAround(ProceedingJoinPoint point, PreInnerAuth preInnerAuth) throws Throwable {
        String source = ServletUtils.getRequest().getHeader(SecurityConstants.FROM_SOURCE);
        // 内部请求验证
        if (!source.equals(SecurityConstants.INNER)) {
            GraceException.display(ResponseType.INNER_FORBIDDEN);
        }
        return point.proceed();
    }

    /**
     * 权限认证执行前执行
     */
    @Override
    public int getOrder()
    {
        return Ordered.HIGHEST_PRECEDENCE + 1;
    }
}
